﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Framework;

namespace Enterprise
{
    /// <summary>
    /// 应聘信息管理
    /// </summary>
    public class RecruitmentService : BaseService
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        public Table GetTable(string post, int pageValue, int rowsValue)
        {
            var parameters = new[]
                             {
                               new SqlParameter("@CoPost", SqlDbType.VarChar, 255)
                             };
            parameters[0].Value = post.SqlLike();
            var strWhere = new StringBuilder();
            strWhere.Append(" where  CoPost like '%'+@CoPost+'%'  ");
            int total = DbHelperSql.GetSingle("select count(*) from TbRecruitment {0}".FormatWith(strWhere), parameters).SafeToInt();
            var sbSql = new StringBuilder();
            sbSql.AppendFormat("select top {0} * from TbRecruitment {1} ", rowsValue, strWhere);
            if (pageValue > 1)
                sbSql.AppendFormat(" and CoRecruitmentId not in (select top {0} CoRecruitmentId from TbRecruitment {1} order by CoRecruitmentId) ", rowsValue * (pageValue - 1), strWhere);
            sbSql.Append(" order by CoRecruitmentId");
            var ds = DbHelperSql.Query(sbSql.ToString(), parameters);
            var table = new Table
            {
                Rows = ds.Tables["ds"].Rows,
                Total = total,
                DataTable = ds.Tables["ds"]

            };
            return table;
        }
        /// <summary>
        /// 根据ID获取一条记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataRow GetById(int id)
        {
            return DbHelperSql.Query("select *  from TbRecruitment  where CoRecruitmentId={0}".FormatWith(id)).Tables["ds"].Rows[0];
        }
        public int Add(string post, string postDescription, string number, string monthlySalary,
                       string workPlace, string workExperience, string education, string age, DateTime date,
                       DateTime validDate, string language, string assess, string candidates, string email,
                       string contact, string otherTreatment, string qq, string workType, string sex)
        {
            Check.Argument.IsNotOutOfLength(post, 25, true, "应聘岗位");
            Check.Argument.IsNotInvalidEmail(email, true, "邮箱");
            Check.Argument.IsNotInvalidSqlDateTime(validDate, "有效期");
            Check.Argument.IsNotInvalidSqlDateTime(date, "发布日期");
            var strSqlOne = new StringBuilder();
            strSqlOne.Append("insert into TbRecruitment(");
            strSqlOne.Append("CoPost,CoPostDescription,CoNumber,CoMonthlySalary,CoWorkPlace,CoWorkExperience,CoEducation,CoAge,CoDate,CoValidDate,CoLanguage,CoAssess,CoCandidates,CoEmail,CoContact,CoOtherTreatment,CoQq,CoWorkType,CoSex");
            strSqlOne.Append(")");
            strSqlOne.Append(" values (");
            strSqlOne.Append("@CoPost,@CoPostDescription,@CoNumber,@CoMonthlySalary,@CoWorkPlace,@CoWorkExperience,@CoEducation,@CoAge,@CoDate,@CoValidDate,@CoLanguage,@CoAssess,@CoCandidates,@CoEmail,@CoContact,@CoOtherTreatment,@CoQq,@CoWorkType,@CoSex");
            strSqlOne.Append(")");
            SqlParameter[] parametersOne =
                {
                    new SqlParameter("@CoPost", SqlDbType.VarChar),
                    new SqlParameter("@CoPostDescription", SqlDbType.VarChar),
                    new SqlParameter("@CoNumber", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoMonthlySalary", SqlDbType.VarChar, 255),  
                    new SqlParameter("@CoWorkPlace", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoWorkExperience", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoEducation", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoAge", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoDate", SqlDbType.DateTime),
                    new SqlParameter("@CoValidDate", SqlDbType.DateTime),
                    new SqlParameter("@CoLanguage", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoAssess", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoCandidates", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoEmail", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoContact", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoOtherTreatment", SqlDbType.VarChar),
                    new SqlParameter("@CoQq", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoWorkType", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoSex", SqlDbType.VarChar, 5)
                };
            parametersOne[0].Value = post;
            parametersOne[1].Value = postDescription;
            parametersOne[2].Value = number;
            parametersOne[3].Value = monthlySalary;
            parametersOne[4].Value = workPlace;
            parametersOne[5].Value = workExperience;
            parametersOne[6].Value = education;
            parametersOne[7].Value = age;
            parametersOne[8].Value = date;
            parametersOne[9].Value = validDate;
            parametersOne[10].Value = language;
            parametersOne[11].Value = assess;
            parametersOne[12].Value = candidates;
            parametersOne[13].Value = email;
            parametersOne[14].Value = contact;
            parametersOne[15].Value = otherTreatment;
            parametersOne[16].Value = qq;
            parametersOne[17].Value = workType;
            parametersOne[18].Value = sex;
            var obj = DbHelperSql.Add(strSqlOne.ToString(), parametersOne).SafeToInt32();
            return obj;
        }
        public int Edit(int id, string post, string postDescription, string number, string monthlySalary,
                       string workPlace, string workExperience, string education, string age, DateTime date,
                       DateTime validDate, string language, string assess, string candidates, string email,
                       string contact, string otherTreatment, string qq, string workType, string sex)
        {
            Check.Argument.IsNotOutOfLength(post, 25, true, "应聘岗位");
            Check.Argument.IsNotInvalidEmail(email, true, "邮箱");
            Check.Argument.IsNotInvalidSqlDateTime(validDate, "有效期");
            Check.Argument.IsNotInvalidSqlDateTime(date, "发布日期");
            var strSqlOne = new StringBuilder();
            strSqlOne.Append("update TbRecruitment set ");
            strSqlOne.Append("CoPost=@CoPost");
            strSqlOne.Append(",");
            strSqlOne.Append("CoPostDescription=@CoPostDescription");
            strSqlOne.Append(",");
            strSqlOne.Append("CoNumber=@CoNumber");
            strSqlOne.Append(",");
            strSqlOne.Append("CoMonthlySalary=@CoMonthlySalary");
            strSqlOne.Append(",");
            strSqlOne.Append("CoWorkPlace=@CoWorkPlace");
            strSqlOne.Append(",");
            strSqlOne.Append("CoWorkExperience=@CoWorkExperience");
            strSqlOne.Append(",");
            strSqlOne.Append("CoEducation=@CoEducation");
            strSqlOne.Append(",");
            strSqlOne.Append("CoAge=@CoAge");
            strSqlOne.Append(",");
            strSqlOne.Append("CoDate=@CoDate");
            strSqlOne.Append(",");
            strSqlOne.Append("CoValidDate=@CoValidDate");
            strSqlOne.Append(",");
            strSqlOne.Append("CoLanguage=@CoLanguage");
            strSqlOne.Append(",");
            strSqlOne.Append("CoAssess=@CoAssess");
            strSqlOne.Append(",");
            strSqlOne.Append("CoCandidates=@CoCandidates");
            strSqlOne.Append(",");
            strSqlOne.Append("CoEmail=@CoEmail");
            strSqlOne.Append(",");
            strSqlOne.Append("CoContact=@CoContact");
            strSqlOne.Append(",");
            strSqlOne.Append("CoOtherTreatment=@CoOtherTreatment");
            strSqlOne.Append(",");
            strSqlOne.Append("CoQq=@CoQq");
            strSqlOne.Append(",");
            strSqlOne.Append("CoWorkType=@CoWorkType");
            strSqlOne.Append(",");
            strSqlOne.Append("CoSex=@CoSex");
            strSqlOne.Append(" where CoRecruitmentId=@id;");
            SqlParameter[] parametersOne =
                {
                    new SqlParameter("@CoPost", SqlDbType.VarChar),
                    new SqlParameter("@CoPostDescription", SqlDbType.VarChar),
                    new SqlParameter("@CoNumber", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoMonthlySalary", SqlDbType.VarChar, 255),  
                    new SqlParameter("@CoWorkPlace", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoWorkExperience", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoEducation", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoAge", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoDate", SqlDbType.DateTime),
                    new SqlParameter("@CoValidDate", SqlDbType.DateTime),
                    new SqlParameter("@CoLanguage", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoAssess", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoCandidates", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoEmail", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoContact", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoOtherTreatment", SqlDbType.VarChar),
                    new SqlParameter("@CoQq", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoWorkType", SqlDbType.VarChar, 255),
                    new SqlParameter("@CoSex", SqlDbType.VarChar, 5),
                    new SqlParameter("@id", SqlDbType.Int)
                };
            parametersOne[0].Value = post;
            parametersOne[1].Value = postDescription;
            parametersOne[2].Value = number;
            parametersOne[3].Value = monthlySalary;
            parametersOne[4].Value = workPlace;
            parametersOne[5].Value = workExperience;
            parametersOne[6].Value = education;
            parametersOne[7].Value = age;
            parametersOne[8].Value = date;
            parametersOne[9].Value = validDate;
            parametersOne[10].Value = language;
            parametersOne[11].Value = assess;
            parametersOne[12].Value = candidates;
            parametersOne[13].Value = email;
            parametersOne[14].Value = contact;
            parametersOne[15].Value = otherTreatment;
            parametersOne[16].Value = qq;
            parametersOne[17].Value = workType;
            parametersOne[18].Value = sex;
            parametersOne[19].Value = id;
            var obj = DbHelperSql.ExecuteSql(strSqlOne.ToString(), parametersOne).SafeToInt();
            return obj;
        }
        public void Delete(int id)
        {
            DbHelperSql.ExecuteSql("delete from TbRecruitment where CoRecruitmentId={0}".FormatWith(id));
        }
    }
}